package com.ossjk.oa.attendance.controller;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ossjk.core.base.controller.BaseController;
import com.ossjk.core.util.CommonUtil;
import com.ossjk.oa.attendance.entity.Atdreport;
import com.ossjk.oa.attendance.service.IAtdreportService;
import com.ossjk.oa.system.entity.Department;
import com.ossjk.oa.system.service.IDepartmentService;
import com.ossjk.oa.system.service.IEmployeeService;

/**
 * <p>
 * 考勤申报表 前端控制器
 * </p>
 *
 * @author chair
 * @since 2019-01-14
 */
@RestController
@RequestMapping("/attendance/goout")
public class GooutController extends BaseController {
	@Autowired
	private IAtdreportService iAtdreportService;
	@Autowired
	private IDepartmentService iDepartmentService;
	@Autowired
	private IEmployeeService iEmployeeService;

	@RequiresPermissions("00050030")
	@GetMapping("/list")
	public Object list(String did, String eid, String date, String rwst, Page page) {
		Wrapper wrapper = Condition.create().isWhere(true).eq("a.type", "5").eq("a.isdelete", "1").orderBy("a.rwst", true).orderBy("a.crtm", true);

		if (!CommonUtil.isBlank(did)) {
			wrapper.eq("a.did", did);
		}
		if (!CommonUtil.isBlank(eid)) {
			wrapper.eq("a.eid", eid);
		} else {
			// 默认查询自己
			if (CommonUtil.isBlank(did)) {
				wrapper.eq("a.eid", getCurrnetUserId());
			}
		}
		if (!CommonUtil.isBlank(date)) {
			wrapper.eq("a.date", date);
		}
		if (!CommonUtil.isBlank(rwst)) {
			wrapper.eq("a.rwst", rwst);
		}
		page = iAtdreportService.selectVoPage(page, wrapper);
		return responseSuccess(page);
	}

	@RequiresPermissions("000500300000")
	@GetMapping("/toInsert")
	public Object toUpdate(Page page) {
		return responseSuccess(iEmployeeService.selectById(getCurrnetUserId()));
	}

	@RequiresPermissions("000500300000")
	@PostMapping("/insert")
	public Object insert(Atdreport atdreport) {
		String[] strNow1 = new SimpleDateFormat("yyyy-MM-dd").format(atdreport.getDate()).toString().split("-");
		atdreport.setYear(strNow1[0]);
		atdreport.setMonth(strNow1[1]);
		atdreport.setDay(strNow1[2]);
		// 查询该员工的部门
		Department department = iDepartmentService.selectByEid(atdreport.getEid());
		// 设置部门id
		atdreport.setDid(department.getId());
		// 审核状态：1-待审核、2-通过、3-不通过
		atdreport.setRwst(new BigDecimal(1));
		if (iAtdreportService.insert(atdreport)) {
			return responseSuccess();
		} else {
			return responseFail();
		}
	}

	@RequiresPermissions("000500300015")
	@GetMapping("/toUpdate/{id}")
	public Object toUpdate(@PathVariable("id") String id) {
		return responseSuccess(iAtdreportService.selectById(id));
	}

	@RequiresPermissions("000500300015")
	@PutMapping("/update")
	public Object update(Atdreport atdreport) {
		if (atdreport.getRwst() != new BigDecimal(1)) {
			atdreport.setRwtm(new Date());
			atdreport.setRwid(getCurrnetUserId());
		}
		if (iAtdreportService.updateById(atdreport)) {
			return responseSuccess();
		} else {
			return responseFail();
		}

	}

	@RequiresPermissions("000500300010")
	@DeleteMapping("/delete/{id}")
	public Object delete(@PathVariable("id") String[] id) {
		if (iAtdreportService.deleteBatchIds(Arrays.asList(id))) {
			return responseSuccess();
		} else {
			return responseFail();
		}
	}
}
